import Pagination from '@/components/pagination';
import mixin from '@/service/mixin';
import {keyMap} from '@/config';
import SelectAppList from '@/components/select-app-list';
import SelectKanaList from '@/components/select-kana-list';
import cancelRepayDialog from '../components/cancel-repay-record'

export default {
    name: 'repayNotRepay',
    components: {
        Pagination,
        SelectAppList,
        SelectKanaList,
        cancelRepayDialog
    },
    mixins: [mixin],
    data() {
        return {
            domain:'',
            // rowData:{},
            form: {
                orderId: '',
                userPhone: '',
                repayBankAccount: '',
                userId: '',
                createTime: '',
                actualRepayTime: '',
                product: '',
                repayStatus: '',
                status: '',
                contractNumber: '',
                appName: '',
                kana:'',
                whiteProductName:'',
                repayChannel: '',
                cancelStatus: '',

                channelCompany: '',
                receiptType:''
            },
            rules: {
                orderId: [],
                userPhone: [],
                repayBankAccount: [],
                userId: [],
                createTime: [],
                actualRepayTime: [],
                status: [],
                contractNumber: [],
            },
            payChannelList: [],
            payWayList: [],
            overdueType: keyMap.overdueType,
            keyMap,
        };
    },
    methods: {
        cancelRepaySub(){
            this.getTableData();
        },
        showCacelRepay(data){
            this.$refs.cancelRepay.changeDialog(data);
            // this.rowData = data
        },
        getTableData(page = this.page) {
            const params = this.fillParams(page);
            this.$api.finance.repay.receiptRecordList(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    this.tableData = data.resultData;
                    this.setPage(data.page, this.page);
                }
            });
        },
        search() {
            this.page.currentPage = 1;
            this.getTableData();
        },
        exportExcel() {
            // if(this.tableData.length<=0)return
            const params = this.fillParams();
            const user = this.$storage.get('user');
            const language = this.$storage.getLocal('language');
            params.language = language;
            params.token = user.token;
            const {$apiConf} = this;
            const url = this.$urlUtil.fillParams(params, $apiConf.baseUrl + $apiConf.urlType.finance.repay.receiptRecordListExcel, ['page']);
            this.$openNewBlank(url);
        },
        fillParams(page = this.page) {
            const form = this.form;
            console.warn(form,'form')
            const params = {
                orderId: '',
                userPhone: '',
                repayBankAccount: '',
                userId: '',
                createTimeS: '',
                createTimeE: '',
                actualRepayTimeS: '',
                actualRepayTimeE: '',
                product: '',
                repayStatus: '',
                cancelStatus:"",
                status: '',
                contractNumber: '',
                appName: '',
                kana:'',
                whiteProductName:'',
                repayChannel: '',
                channelCompany: '',
                receiptType:'',
                page: {
                    pageNo: page.currentPage,
                    pageSize: this.page.pageSize
                }
            };
            Object.keys(form).forEach(key => {
                if (typeof form[key] === 'string' && form[key] && form[key]) {
                    form[key] = form[key].trim();
                    params[key] = form[key];
                }
            });
            if (form.createTime) {
                params.createTimeS = form.createTime[0];
                params.createTimeE = form.createTime[1];
            }
            if (form.actualRepayTime) {
                params.actualRepayTimeS = form.actualRepayTime[0];
                params.actualRepayTimeE = form.actualRepayTime[1];
            };
            console.warn(params)
            return params;
        },
        getChannelPayWay(type) {
            const params = {};
            if (type === 'repayWay') {
                params.type = 0
            }else if (type === 'repayChannel') {
                if(this.form.channelCompany) {
                    this.form.repayChannel = '';
                    params.type = 1;
                    params.payType = 1;
                    params.channelCode = this.form.channelCompany;
                }
                else {
                    this.getAllChannelPayWay();
                    return;
                }
            }
            this.$api.finance.repay.getChannelPayWay(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                  if (type === 'repayWay') {
                    this.payWayList = data || [];
                  }else if (type === 'repayChannel') {
                    this.payChannelList =  data || [];
                  }
                }
            });
        },
        getAllChannelPayWay() {
            const params = {
                type: 1,
                payType: 1
            };
            this.$api.finance.productSetting.getAllChannelPayWay(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    this.payChannelList =  data;
                }
            });
        }
    },
    created() {
        this.domain = this.$storage.get('user').domain
        this.tableMaxHeight = document.body.clientHeight - 265;
        // this.getTableData();
        this.getChannelPayWay('repayWay');
        this.getAllChannelPayWay();
    }
}